package com.example.wan1.view.search;

import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.SearchHotBean;
import com.example.wan1.model.common.CommonResList;

import java.util.List;

import javax.inject.Inject;

/***     
 * @Author: zxd
 * @CreateDate: 2020/7/28 14:13
 * @Description: 搜索l
 */
public class SearchPresenter implements SearchContract.Presenter {

    @Inject
    SearchContract.View mView;

    @Inject
    public SearchPresenter() {
    }

    @Override
    public void search(int page, String k) {
        RetrofitUtil.getInstance().search(page, k)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

    @Override
    public void searchByKnowledge(int page, String author) {
        RetrofitUtil.getInstance().searchByKnowledge(page, author)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

    @Override
    public void searchByWechat(int wxId, int page, String k) {
        RetrofitUtil.getInstance().searchByWechat(wxId, page, k)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<CommonResList<ArticleBean>>() {
                    @Override
                    public void onSuccess(CommonResList<ArticleBean> result) {
                        mView.onSuccess(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }

    @Override
    public void searchHotData() {
        RetrofitUtil.getInstance().searchHotData()
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<List<SearchHotBean>>() {
                    @Override
                    public void onSuccess(List<SearchHotBean> result) {
                        mView.onGetHotData(result);
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.onFailure(e.getMessage());
                    }
                });
    }
}
